package com.migo.common;

import java.util.*;

/**
 * Created by Wes on 2019/1/17.
 */
public class StringSplitUtil {

    private static class StringSplitUtilHolder {
        private static final StringSplitUtil INSTANCE = new StringSplitUtil();
    }

    private StringSplitUtil() {
    }

    public static final StringSplitUtil getInstance() {
        return StringSplitUtilHolder.INSTANCE;
    }

    public String getLastVersion(String mergeVersion)
    {
        StringBuffer result =new StringBuffer();
        String[] input = mergeVersion.split(",");
        List<String> list = (List<String>) Arrays.asList(input);
        Collections.sort(list);
        int j;
        for (j = 0; j < input.length; j++) {
            String[] firmwareArr = input[j].split("_");
            if ("0".equals(firmwareArr[1].toString())) {
                continue;
            }
            result.append(input[j]);
            if(j<input.length-1)
            {
                result.append("-");
            }
        }
        return result.toString();
    }

    public Map<String,String> getLastVersionMap(String mergeVersion,String splitType)
    {
        Map<String,String> map = new HashMap<>();
        StringBuffer result =new StringBuffer();
        String[] input = mergeVersion.split(splitType);
        List<String> list = (List<String>) Arrays.asList(input);
        Collections.sort(list);
        int j;
        for (j = 0; j < input.length; j++) {
            String[] firmwareArr = input[j].split("_");
            if ("0".equals(firmwareArr[1].toString())) {
                continue;
            }
            map.put(firmwareArr[0],firmwareArr[1]);
        }
        return map;
    }


    public String getMergeVersion(String mergeVersion)
    {
        StringBuffer result =new StringBuffer();
        String[] input = mergeVersion.split(",");
        List<String> list = (List<String>) Arrays.asList(input);
        Collections.sort(list);
        int j;
        for (j = 0; j < input.length; j++) {
            result.append(input[j].split("_")[0]);
            if(j<input.length-1)
            {
                result.append("-");
            }
        }
        return result.toString();
    }
}
